/**
 * Created by liyun on 2016/12/23.
 */
app.controller('LinkCtrl', function($scope, $state, $stateParams, $timeout, BthSevice,$ionicConfig) {
    if(BthSevice.scanned != true){ //没有扫描过直接扫描
        scan(); //开始搜索
    }else {
        $scope.scanned = true;
    }

    function scan() { //扫描蓝牙设备
        BthSevice.startScan(function (res) {
            switch (res.status) {
                case 'ok':
                    // $timeout(function () { //不要切换那么快
                    BthSevice.scanned = true; //设置为扫描过一次
                    $scope.scanned = true;
                    $timeout(function () {
                        BthSevice.stopScan();
                    }, 1000);
                    $state.go('app.MyLinking');

                    // }, 2500);
                    break;
                case 'timeout':
                    // console.log('搜索超时！');
                    break;
                default:
                // console.log('出错了！');
            }

        });
    }
    $scope.retry = function () { //重试
        scan();
        $scope.scanned = false;
    };

    $scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options) {
        $ionicConfig.views.swipeBackEnabled(false);
        if (toState.name != 'app.MyLinking') { //只要不是跳转到选择设备页面就可以显示重新链接的按钮
            BthSevice.scanned = false;
        }
    });
});
